package handlers

import (
	"github.com/gin-gonic/gin"
	"short-vid-mgr/models"
	"strconv"
)

func Add(c *gin.Context) {
	user_id, _ := strconv.Atoi(c.PostForm("user_id"))
	title := c.PostForm("title")
	context := c.PostForm("context")
	like_count, _ := strconv.Atoi(c.PostForm("like_count"))
	video := models.Video{
		UserId:    user_id,
		Title:     title,
		Context:   context,
		LikeCount: like_count,
	}
	err := models.DB.Create(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发布成功",
	})
}

func List(c *gin.Context) {
	var video []models.Video
	err := models.DB.Order("like_count desc").Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

// 用户查看自己关注的的函数
func Find(c *gin.Context) {
	user_id, _ := strconv.Atoi(c.Query("user_id"))
	var video []models.Video
	err := models.DB.Where("user_id=?", user_id).Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})

}
